require "net/http"
require "data/Data"

class ExperimentalData
  DATA_URL="http://people.cs.ubc.ca/~taosu/tacscreen/vkey/service.php?task=getall"
  
  attr_reader :group_by_uid, :group_by_layer, :group_by_set
  attr_accessor :layer1filter
  
  def initialize
    @group_by_uid = {}
    @group_by_layer = {}
    @group_by_set = {}
      
    @layer1filter = {
      2 => [1, 2],
      3 => [1, 2],  
      4 => [8, 7],
      5 => [1, 2],
      6 => [3, 4]      
    }  
      
      
    getData        
  end
    
  
   
  
  #Download the data from the web
  def getData
    @group_by_uid = {}
    @group_by_layer = {}
    @group_by_set = {}
      
    resp = Net::HTTP.get_response(URI.parse(DATA_URL))
    resp.body.each do |s|
      e = Entry.new(s)
      
      #Data correction
      next if e.uid == 1
      next if e.uid == 999
      next if e.layer == 1 && @layer1filter[e.uid].include?(e.set)
      e.uid = 3 if (e.uid == 2 && e.date.day==14 && e.date.mon==3)#  === Date.parse("2010-03-14")
      
      
      
      
      #initialize if nil
      @group_by_uid[e.uid] ||= []
      @group_by_layer[e.layer] ||= []
      @group_by_set[e.set] ||= []  
      
      @group_by_set[e.set] << e
      @group_by_uid[e.uid] << e
      @group_by_layer[e.layer] << e
       
      #puts "#{e.date}, uid:#{e.uid}, layer:#{e.layer}, set:#{e.set}, phrase:#{e.phrase}, typed: #{e.typedSequence}"
    end
    
    
  end
  
  
  
end